package org.rkuk.kasutil.media 
{
	import flash.events.Event;
	import flash.events.NetStatusEvent;
	import org.rkuk.kasutil.designPatternTools.AbstractTool;
	/**
	 * ...
	 * @author rkuk
	 */
	internal class MediaStreamProxy extends MediaProxy 
	{
		/**
		 * 被代理的MediaStream对象
		 */
		private var mediaStream:MediaStream;
		
		public function MediaStreamProxy(mediaStream:MediaStream) 
		{
			AbstractTool.makeAbstractClass(this, MediaStreamProxy);
			this.mediaStream = mediaStream;
			mediaStream.addEventListener(NetStatusEvent.NET_STATUS, onMediaStreamStatus);
		}
		
		override public function dispose():void 
		{
			mediaStream.removeEventListener(NetStatusEvent.NET_STATUS, onMediaStreamStatus);
			mediaStream = null;
		}
		
		/**
		 * 征听MediaStream的NetStatusEvent，并转发出相应的状态
		 * @param	e
		 */
		protected function onMediaStreamStatus(e:NetStatusEvent):void 
		{
			var status:String = e.info.code;
			//只需要征听这几个Status编码
			if (status == MediaStreamStatusEvent.STATUS_BUFFER ||
				//status == MediaStreamStatusEvent.STATUS_COMPLETE ||
				status == MediaStreamStatusEvent.STATUS_FLUSH ||
				status == MediaStreamStatusEvent.STATUS_PAUSE ||
				status == MediaStreamStatusEvent.STATUS_PLAY ||
				status == MediaStreamStatusEvent.STATUS_SEEK ||
				status == MediaStreamStatusEvent.STATUS_STOP)
				
				dispatchEvent(new MediaStreamStatusEvent(status));
		}
		
		/**
		 * @inheritDoc
		 */
		override public function get state():MediaState 
		{
			return mediaStream.state;
		}
		
		/**
		 * @inheritDoc
		 */
		override public function set state(value:MediaState):void 
		{
			mediaStream.state = value;
			mediaStream.dispatchEvent(new MediaSourceEvent(value.description));
		}
		
		/**
		 * @inheritDoc
		 */
		override public function sendMediaEvent(e:Event):void 
		{
			mediaStream.dispatchEvent(e);
		}
		
		/**
		 * @inheritDoc
		 */
		override public function start(...rest):void 
		{
			rest.unshift(mediaStream.streamName);
			mediaStream.startMedia.apply(mediaStream, rest);
		}
		
		/**
		 * @inheritDoc
		 */
		override public function stop():void 
		{
			mediaStream.stopMedia();
		}
		
		/**
		 * @inheritDoc
		 */
		override public function resume():void 
		{
			mediaStream.resumeMedia();
		}
		
		/**
		 * @inheritDoc
		 */
		override public function pause():void 
		{
			mediaStream.pause();
		}
		
		/**
		 * @inheritDoc
		 */
		override public function seek(offset:Number):void 
		{
			mediaStream.seekMedia(offset);
		}
		
		override public function get isBuffering():Boolean 
		{
			return mediaStream.bufferLength < mediaStream.bufferTime;
		}
	}
}